package com.asiainfo.iov.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asiainfo.iov.activity.BaseActivity;
import com.asiainfo.iov.configs.ConstantsME;
import com.asiainfo.iov.configs.EventBusTag;
import com.asiainfo.iov.configs.InterfaceConfig;
import com.asiainfo.iov.networkModel.HttpUtil;
import com.framework.utils.CollectionUtil;
import com.framework.utils.ComparatorUtil;
import com.framework.utils.PreferencesHelper;
import com.framework.utils.Y;
import com.framework2.customLoading.LoadingIndicatorView;
import com.framework2.customviews.SimpleLineChart;
import com.library.percent.PercentRelativeLayout;
import com.yaxin.yyt.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

public class Fragment_linechart2 extends Fragment implements View.OnClickListener {
    private Context mContext;
    private View view;// infalte的布局
    private LinearLayout containerLayout;// 新建容器
    /////////////////////////////////
    /*
     *空布局
     */
    private RelativeLayout loadingLayout;
    private LoadingIndicatorView loadingProgress;
    private TextView tvLoading;
    /*
     *内容布局
     */
    private CardView cardView;
    private PercentRelativeLayout simpleLineChartLayout;
    private LinearLayout emptyLayout;

    private TextView tv_total_phone_sale;
    private TextView tv_total_accessories_sale;
    private SimpleLineChart mSimpleLineChart;
    private boolean requestSuccess;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        if (null == view) {
            containerLayout = new LinearLayout(mContext);
            view = inflater.inflate(R.layout.fragment_linechart_percent, null);
//            view.setMinimumHeight(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getHeight());
//            view.setMinimumWidth(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getWidth());
            initView();
            containerLayout.addView(view);
            EventBus.getDefault().register(this);
        } else {
            containerLayout.removeAllViews();
            containerLayout = new LinearLayout(getActivity());
            containerLayout.addView(view);
        }
//        if (!requestSuccess)
//            requestlast7daysSaleDetail(PreferencesHelper.getInstance().getStringData(ConstantsME.currentShopId));
        return containerLayout;
    }

    //eventBus通知 getShopIdSuccess
    @Subscribe(threadMode = ThreadMode.MAIN, tag = EventBusTag.shouldRefresh)
    public void shouldRefresh(String info) {
//        requestlast7daysSaleDetail(PreferencesHelper.getInstance().getStringData(ConstantsME.currentShopId));
        requestSuccess = false;
    }


    private void initView() {
        tv_total_phone_sale = (TextView) view.findViewById(R.id.tv_total_phone_sale);
        tv_total_accessories_sale = (TextView) view.findViewById(R.id.tv_total_accessories_sale);
        TextView tv_left_txt = (TextView) view.findViewById(R.id.tv_left_txt);
        TextView tv_right_txt = (TextView) view.findViewById(R.id.tv_right_txt);
        tv_left_txt.setText("手机销售总量");
        tv_right_txt.setText("配件销售总量");
        loadingLayout = (RelativeLayout) view.findViewById(R.id.loadingLayout);
        loadingProgress = (LoadingIndicatorView) view.findViewById(R.id.loadingProgress);
        tvLoading = (TextView) view.findViewById(R.id.tvLoading);
        cardView = (CardView) view.findViewById(R.id.cardView);
        simpleLineChartLayout = (PercentRelativeLayout) view.findViewById(R.id.simpleLineChartLayout);
        emptyLayout = (LinearLayout) view.findViewById(R.id.emptyLayout);

        loadingLayout.setOnClickListener(this);

        mSimpleLineChart = (SimpleLineChart) view.findViewById(R.id.simpleLineChart);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loadingLayout:
                loadingProgress.setVisibility(View.VISIBLE);
                tvLoading.setText("加载中");
//                requestlast7daysSaleDetail(PreferencesHelper.getInstance().getStringData(ConstantsME.currentShopId));
                break;
            default:
                break;
        }
    }
}
